
package com.peach.inquiry;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.peach.inquiry.imsdk.LogUtil;
import com.peach.inquiry.imsdk.TimManager;
import com.peach.inquiry.imsdk.tim.ITimUserStatusListener;
import com.peach.inquiry.imsdk.tim.message.MsgBean;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * Handles the method calls for the plugin.
 */
class MethodCallHandler implements MethodChannel.MethodCallHandler {

    private IM im;

    MethodCallHandler(IM im) {
        this.im = im;
    }

    @Override
    public void onMethodCall(MethodCall call, final MethodChannel.Result result) {

        switch (call.method) {

            case "init":
                int timAppID = call.argument("timAppID");
//                LogUtil.e("初始化   " + timAppID);
                im.initSdk(timAppID);
                break;

            case "login":
                String userID = call.argument("userID");
                String userSign = call.argument("userSign");
                TimManager.getInstance().login(userID, userSign);
                break;

            case "logout":
                TimManager.getInstance().logout();
                break;

            case "isLogin":
                int status = TimManager.getInstance().getLoginStatus();
                im.userStatus(status);
                boolean isLogin = status == V2TIMManager.V2TIM_STATUS_LOGINED;
                if (isLogin) {
                    im.afterLoginSuccess();
                }
                result.success(isLogin);
                break;

            case "getChatList":
                long nextSeq = Long.parseLong(call.argument("nextSeq").toString());
                int count = call.argument("count");
                TimManager.getInstance().getConversationList(nextSeq, count);
                break;

            case "getChat":
                String chatID = call.argument("chatID");
                TimManager.getInstance().getConversation(chatID);
                break;

            case "getC2CMsgList":
                String msgUserID = call.argument("userID");
                int msgCount = call.argument("count");
                boolean msgFirstPull = call.argument("firstPull");
                TimManager.getInstance().getC2CHistoryMsgList(msgUserID, msgCount, msgFirstPull);
                break;

            case "sendMsg":
//                LogUtil.e("发送消息内容" + new Gson().toJson(call.arguments));
//                String msgBeanJson = call.argument("msg");
                MsgBean msgBean = new Gson().fromJson(call.arguments.toString(), MsgBean.class);
                LogUtil.e("发送消息内容" + new Gson().toJson(msgBean));
                if (msgBean.textElem != null && !msgBean.textElem.isEmpty()) {
                    TimManager.getInstance().sendTextMsg(msgBean.userID, msgBean.textElem, msgBean.customMsgID);
                } else if (msgBean.imgPath != null && !msgBean.imgPath.isEmpty()) {
                    TimManager.getInstance().sendImgMsg(msgBean.userID, msgBean.imgPath, msgBean.customMsgID);
                } else if (msgBean.soundPath != null && !msgBean.soundPath.isEmpty()) {
                    TimManager.getInstance().sendVoiceMsg(msgBean.userID, msgBean.soundPath, msgBean.soundDuration, msgBean.customMsgID);
                }
                break;

            case "getSoundPath":
                String getSoundPathMsgID = call.argument("msgID");
//                LogUtil.e("获取语音消息Path" + getSoundPathMsgID);
                im.getSoundPath(getSoundPathMsgID);
                break;

            case "markC2CMessageAsRead":
                String markC2CMessageAsReadUserID = call.argument("userID");
                LogUtil.e("设置单聊消息已读" + markC2CMessageAsReadUserID);
                TimManager.getInstance().markC2CMessageAsRead(markC2CMessageAsReadUserID);
                break;

            default:
                result.notImplemented();
                break;
        }
    }

}
